﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web.Mvc;

namespace EasyUIMvc
{
    public class Test : HtmlHelper { 
        
    }
    public static class HtmHelperExtension
    {
        //
        // Summary:
        //     Provides access to helper methods allowing you to render individual DevExpress
        //     ASP.NET MVC extensions.
        //
        // Parameters:
        //   helper:
        //     A System.Web.Mvc.HtmlHelper object representing the HTML helper instance
        //     that this method extends.
        //
        // Returns:
        //     An DevExpress.Web.Mvc.UI.ExtensionsFactory object containing helper methods
        //     to render DevExpress MVC extensions.
        public static ExtensionsFactory EasyUI(this HtmlHelper helper)
        {
            return ExtensionsFactory.Instance;
        }
    }

    public class ExtensionsFactory
    {
        private static ExtensionsFactory _Instance;
        public static ExtensionsFactory Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new ExtensionsFactory();
                return _Instance;
            }
        }

        public MenuExtension Menu(Action<MenuSettings> method)
        {
            return CreateExtension<MenuExtension, MenuSettings>(method);
        }
        public MenuButtonExtension MenuButton(Action<MenuButtonSettings> method)
        {
            return CreateExtension<MenuButtonExtension, MenuButtonSettings>(method);
        }
        public LinkButtonExtension LinkButton(Action<LinkButtonSettings> method)
        {
            return CreateExtension<LinkButtonExtension, LinkButtonSettings>(method);
        }
        public ComboBoxExtension ComboBox(Action<ComboBoxSettings> method)
        {
            return CreateExtension<ComboBoxExtension, ComboBoxSettings>(method);
        }
        public CalendarExtension Calendar(Action<CalendarSettings> method)
        {
            return CreateExtension<CalendarExtension, CalendarSettings>(method);
        }
        public WindowExtension Window(Action<WindowSettings> method)
        {
            return CreateExtension<WindowExtension, WindowSettings>(method);
        }
        public DataGridExtension DataGrid(Action<DataGridSettings> method)
        {
            return CreateExtension<DataGridExtension, DataGridSettings>(method);
        }
        public TreeExtension Tree(Action<TreeSettings> method)
        {
            return CreateExtension<TreeExtension, TreeSettings>(method);
        }

        internal static TE CreateExtension<TE, TS>(Action<TS> method)
            where TE : ExtensionBase
            where TS : SettingsBase, new()
        {
            var settings = Activator.CreateInstance<TS>();
            method(settings);
            return (TE)CreateExtension(typeof(TE), settings);
        }

        internal static ExtensionBase CreateExtension(Type type, SettingsBase settings)
        {
            return (ExtensionBase)Activator.CreateInstance(type, new object[1] { settings });
        }
    }

    public abstract class ExtensionBase
    {
        protected virtual TagBuilder GetTopTag(string tag, string cssCls,SettingsBase settings)
        {
            var top = new TagBuilder(tag);
            top.AddCssClass(cssCls);
            top.GenerateId(settings.Id);

            if (!string.IsNullOrWhiteSpace(settings.DataOptions))
            {
                top.Attributes.Add("data-options",settings.DataOptions);
            }

            return top;
        }

        public abstract string GetScripts();
        public abstract MvcHtmlString GetHtml();
    }

    public abstract class SettingsBase
    {
        /// <summary>
        /// 即html标签中的id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 用来设置html标签中的data-options属性
        /// </summary>
        public string DataOptions { get; set; }
    }
}
